package com.bls.sms.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bls.sms.bo.interfaces.ClassBO;
import com.bls.sms.exception.ApplicationException;
import com.bls.sms.struts.form.ClassForm;
import com.bls.sms.wrapper.ClassWrapper;

public class ClassAction extends BaseAction {
    private ClassBO classBO = null;

    public ActionForward addClass(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            ClassForm form = (ClassForm) actionForm;
            ClassWrapper wrapper = new ClassWrapper();
            setWrapperValues(form, wrapper);
            getClassBO().saveClass(wrapper);
        } catch (ApplicationException e) {
            e.printStackTrace();
        }
        return actionMapping.findForward("success");
    }
    public ActionForward updateClass(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            ClassForm form = (ClassForm) actionForm;
            ClassWrapper wrapper = null;
            setWrapperValues(form, wrapper);
            getClassBO().updateClass(wrapper);
        } catch (ApplicationException e) {
            e.printStackTrace();
        }
        return actionMapping.findForward("success");
    }
    public ActionForward deleteClass(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            ClassForm form = (ClassForm) actionForm;
            ClassWrapper wrapper = null;
            setWrapperValues(form, wrapper);
            getClassBO().deleteClass(wrapper);
        } catch (ApplicationException e) {
            e.printStackTrace();
        }
        return actionMapping.findForward("success");
    }
    public ActionForward listClasses(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            List<ClassWrapper> list = getClassBO().loadClasses();
            request.setAttribute("list", list);
        } catch (ApplicationException e) {
            e.printStackTrace();
        }
        return actionMapping.findForward("success_list_classes");
    }
    private void setWrapperValues(ClassForm form, ClassWrapper wrapper) {
        if (wrapper == null) {
            wrapper = new ClassWrapper();
        }
        wrapper.setId(form.getId());
        wrapper.setName(form.getName());
        wrapper.setRoomNo(form.getRoomNo());
        wrapper.setSection(form.getSection());
        wrapper.setTeacher(form.getTeacher());
    }
    public ClassBO getClassBO() {
        return classBO;
    }
    public void setClassBO(ClassBO classBO) {
        this.classBO = classBO;
    }
}
